#!/bin/bash
#设置任务异常退出
set -eo pipefail
dt='{{ execution_date | cst_ds }}'
name='网点滞留件汇总临时导数'
# 在退出时清理文件
trap "rm -f /tmp/${name}_${dt}.zip /tmp/${name}_${dt}.csv && echo 'removed tmp files'" EXIT
# 创建txt文件
touch /tmp/${name}_${dt}.txt
#将中文字段名输入文件顶部
#将查询结果追加到文件中
{
echo -e "是否包含问题件Y是N否\t管理大区\t代理区编码\t代理区名字\t省\t市\t区县\t加盟商\t网点编码\t网点名字\t一天应签收\t一天内滞留\t两天应签收\t两天内滞留\t三天应签收\t三天内滞留\t五天应签收\t五天内滞留\t七天应签收\t七天内滞留\t十天应签收\t十天内滞留\t十五天应签收\t十五天内滞留\t日期\t">/tmp/${name}_${dt}.txt;
spark-sql --driver-memory 10G --num-executors 40 --executor-memory 8g --executor-cores 4  --conf spark.executor.memoryOverhead=4g --conf spark.driver.maxResultSize=6g -S -e "
with terminal_sign_summary_plan_sign as(
     select order_source_name,--订单来源名称
       goods_type_code,--物品类型编码
       end_manage_region_code,--末端网点所属管理大区编码
       max(end_manage_region_name) as end_manage_region_name,--末端网点所属管理大区名称
       end_agent_code,--末端网点所属代理区编码
       max(end_agent_name) as end_agent_name,--末端网点所属代理区名称
       max(end_provider_id) as end_provider_id,--末端网点所属省份id
       max(end_provider_name) as end_provider_name,--末端网点所属省份名称
       max(end_city_id) as end_city_id,--末端网点所属城市id
       max(end_city_name) as end_city_name,--末端网点所属城市名称
       max(end_area_id) as end_area_id,--末端网点所属区县id
       max(end_area_name) as end_area_name,--末端网点所属区县名称
       end_center_code,--末端发件中心/集散编码
       max(end_center_name) as end_center_name,--末端发件中心/集散名称
       end_franchisee_code,--末端网点所属加盟商编码
       max(end_franchisee_name) as end_franchisee_name,--末端网点所属加盟商名称
       final_sign_network_code,--最终签收派件网点编码
       max(final_sign_network_name) as final_sign_network_name,--最终签收派件网点名称
       max(planned_delivery_time) as planned_delivery_time,--规划发件时间
       max(deadline_signing_time) as deadline_signing_time,--规划签收截止时间
      -- max(span_days) as span_days,--加时天数
       deliver_shift,--派件派仓班次
       final_sign_user_code,--最后签收派件员编码
       max(final_sign_user_name) as final_sign_user_name,--最后签收派件员名称
        sum(1) as need_sign_count,--应签收汇总
        sum(case when aging_sign_count_type = 25 then 1 else 0 end) as aging_sign_over_time_difficult,--时效签收延误-问题件
        sum(case when aging_sign_count_type = 26 then 1 else 0 end) as aging_sign_over_time_other,--时效签收延误-其他
         final_plan_sign_date,--规划签收日期
       final_plan_sign_date as dt
from (select
         date_format(final_plan_sign_time, 'yyyy-MM-dd') as final_plan_sign_date,--规划签收日期
       case when aging_sign_time is null and is_difficult = 1 then 25
            when aging_sign_time is null then 26
            when aging_sign_time <= final_plan_sign_time and aging_sign_scan_type = '快件签收' then 11
            when aging_sign_time <= final_plan_sign_time and aging_sign_scan_type = '代理点收入扫描' then 14
            when aging_sign_time <= final_plan_sign_time and is_aging_sign_pdd = 1 then 12
            when aging_sign_time <= final_plan_sign_time and is_aging_sign_pdd = 2 then 13
            when aging_sign_time > final_plan_sign_time and aging_sign_scan_type = '快件签收' then 21
            when aging_sign_time > final_plan_sign_time and aging_sign_scan_type = '代理点收入扫描' then 24
            when aging_sign_time > final_plan_sign_time and is_aging_sign_pdd = 1 then 22
            when aging_sign_time > final_plan_sign_time and is_aging_sign_pdd = 2 then 23
            else 26 end aging_sign_count_type,--时效签收统计类型 注：11 准点-网点签收 12 准点-终端入库(桃花岛认证) 13 准点-终端入库(自助对接) 14 准点-代理点收入 21 延误-网点签收 22 延误-终端入库(桃花岛认证) 23 延误-终端入库(自助对接) 24 延误-代理点收入 25 延误-问题件 26 延误-其他
       nvl(order_source_name,'其他') as order_source_name,
       nvl(goods_type_code,'bm000006') as goods_type_code,
       billcode,--运单号
       nvl(end_manage_region_code,'-') as end_manage_region_code,--末端网点所属管理大区编码
       nvl(end_manage_region_name,'-') as end_manage_region_name,--末端网点所属管理大区名称
       nvl(end_agent_code,'-') as end_agent_code,--末端网点所属代理区编码
       nvl(end_agent_name,'-') as end_agent_name,--末端网点所属代理区名称
       nvl(end_provider_id,0) as end_provider_id,--末端网点所属省份id
       nvl(end_provider_name,'-') as end_provider_name,--末端网点所属省份名称
       nvl(end_city_id,0) as end_city_id,--末端网点所属城市id
       nvl(end_city_name,'-') as end_city_name,--末端网点所属城市名称
       nvl(end_area_id,0) as end_area_id,--末端网点所属区县id
       nvl(end_area_name,'-') as end_area_name,--末端网点所属区县名称
       nvl(end_franchisee_code,'-') as end_franchisee_code,--末端网点所属加盟商编码
       nvl(end_franchisee_name,'-') as end_franchisee_name,--末端网点所属加盟商名称
       nvl(end_center_code,'-') as end_center_code,--末端发件中心/集散编码
       nvl(end_center_name,'-') as end_center_name,--末端发件中心/集散名称
       nvl(end_center_send_time,'-') as end_center_send_time,--末端中心/集散发件时间
       nvl(final_sign_network_code,'-') as final_sign_network_code,--最终签收派件网点编码
       nvl(final_sign_network_name,'-') as final_sign_network_name,--最终签收派件网点名称
       final_plan_sign_time,--规划签收时间
       planned_delivery_time,--规划发件时间
       deadline_signing_time,--规划签收截止时间
       deliver_shift,--派件派仓班次
       nvl(final_sign_user_code,'-') as final_sign_user_code,--最后签收派件员编码
       nvl(final_sign_user_name,'-') as final_sign_user_name,--最后签收派件员名称
       difficult_first_type,--问题件一级类型
  dt
from jms_dwd.dwd_terminal_sign_all_detail_new_dt
where dt between date_format(date_add('{{ execution_date | cst_ds }}', -60), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', 0), 'yyyy-MM-dd')
  and to_date(final_plan_sign_time) between date_format(date_add('{{ execution_date | cst_ds }}', -20), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', 0), 'yyyy-MM-dd')) a
group by final_plan_sign_date,
         order_source_name,
         goods_type_code,
         end_manage_region_code,
         end_agent_code,
         end_center_code,
         end_franchisee_code,
         final_sign_network_code,
         deliver_shift,
         final_sign_user_code
     )

select is_contain_difficult,  --是否包含问题件 1 是 2 否
       end_manage_region_name,  --末端网点所属管理大区名称
       end_agent_code,  --末端网点所属代理区编码
       end_agent_name,  --末端网点所属代理区名称
       end_provider_name,  --末端网点所属省份名称
       end_city_name,  --末端网点所属城市名称
       end_area_name,  --末端网点所属区县名称
       end_franchisee_name,  --末端网点所属加盟商名称
       final_sign_network_code,  --最终签收派件网点编码
       final_sign_network_name,  --最终签收派件网点名称
       need_sign_count_1d,  --1天应签收
       retention_count_1d,  --1天内滞留
       need_sign_count_2d,  --2天应签收
       retention_count_2d,  --2天内滞留
       need_sign_count_3d,  --3天应签收
       retention_count_3d,  --3天内滞留
       need_sign_count_5d,  --5天应签收
       retention_count_5d,  --5天内滞留
       need_sign_count_7d,  --7天应签收
       retention_count_7d,  --7天内滞留
       need_sign_count_10d, --10天应签收
       retention_count_10d, --10天内滞留
       need_sign_count_15d, --15天应签收
       retention_count_15d, --15天内滞留
       final_plan_sign_date  --规划签收日期
       -- final_plan_sign_date as dt
from (
       select 'Y' as is_contain_difficult,
              max(end_manage_region_code) as end_manage_region_code,
              max(end_manage_region_name) as end_manage_region_name,
              max(end_agent_code) as end_agent_code,
              max(end_agent_name) as end_agent_name,
              max(end_provider_id) as end_provider_id,
              max(end_provider_name) as end_provider_name,
              max(end_city_id) as end_city_id,
              max(end_city_name) as end_city_name,
              max(end_area_id) as end_area_id,
              max(end_area_name) as end_area_name,
              max(end_franchisee_code) as end_franchisee_code,
              max(end_franchisee_name) as end_franchisee_name,
              final_sign_network_code,
              max(final_sign_network_name) as final_sign_network_name,
              sum(case when final_plan_sign_date = date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_1d,
              sum(case when final_plan_sign_date = date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then (aging_sign_over_time_other + aging_sign_over_time_difficult) else 0 end) as retention_count_1d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -2), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_2d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -2), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then (aging_sign_over_time_other + aging_sign_over_time_difficult) else 0 end) as retention_count_2d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -3), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_3d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -3), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then (aging_sign_over_time_other + aging_sign_over_time_difficult) else 0 end) as retention_count_3d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -5), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_5d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -5), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then (aging_sign_over_time_other + aging_sign_over_time_difficult) else 0 end) as retention_count_5d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -7), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_7d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -7), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then (aging_sign_over_time_other + aging_sign_over_time_difficult) else 0 end) as retention_count_7d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -10), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_10d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -10), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then (aging_sign_over_time_other + aging_sign_over_time_difficult) else 0 end) as retention_count_10d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -15), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_15d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -15), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') then (aging_sign_over_time_other + aging_sign_over_time_difficult) else 0 end) as retention_count_15d,
              date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') as final_plan_sign_date
       from terminal_sign_summary_plan_sign
       where dt between date_format(date_add('{{ execution_date | cst_ds }}', -20), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd')
       group by final_sign_network_code
       union all
       select 'N' as is_contain_difficult,
              max(end_manage_region_code) as end_manage_region_code,
              max(end_manage_region_name) as end_manage_region_name,
              max(end_agent_code) as end_agent_code,
              max(end_agent_name) as end_agent_name,
              max(end_provider_id) as end_provider_id,
              max(end_provider_name) as end_provider_name,
              max(end_city_id) as end_city_id,
              max(end_city_name) as end_city_name,
              max(end_area_id) as end_area_id,
              max(end_area_name) as end_area_name,
              max(end_franchisee_code) as end_franchisee_code,
              max(end_franchisee_name) as end_franchisee_name,
              final_sign_network_code,
              max(final_sign_network_name) as final_sign_network_name,
              sum(case when final_plan_sign_date = date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_1d,
              sum(case when final_plan_sign_date = date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then aging_sign_over_time_other else 0 end) as retention_count_1d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_2d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then aging_sign_over_time_other else 0 end) as retention_count_2d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -2), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_3d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -2), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then aging_sign_over_time_other else 0 end) as retention_count_3d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -4), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_5d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -4), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then aging_sign_over_time_other else 0 end) as retention_count_5d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -6), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_7d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -6), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then aging_sign_over_time_other else 0 end) as retention_count_7d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -9), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then need_sign_count else 0 end) as need_sign_count_10d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -9), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then aging_sign_over_time_other else 0 end) as retention_count_10d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -14), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then need_sign_count else 0 end) as            need_sign_count_15d,
              sum(case when final_plan_sign_date between date_format(date_add('{{ execution_date | cst_ds }}', -14), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd') then aging_sign_over_time_other else 0 end) as retention_count_15d,
              date_format(date_add('{{ execution_date | cst_ds }}', -1), 'yyyy-MM-dd') as final_plan_sign_date
       from terminal_sign_summary_plan_sign
       where dt between date_format(date_add('{{ execution_date | cst_ds }}', -20), 'yyyy-MM-dd') and date_format(date_add('{{ execution_date | cst_ds }}', -0), 'yyyy-MM-dd')
       group by final_sign_network_code
) t;
   " >>/tmp/${name}_${dt}.txt
}
# 替换分隔符，并输出csv
cat /tmp/${name}_${dt}.txt | tr -s '\t' ',' > /tmp/${name}_${dt}.csv
# 压缩并删除 csv 文件
zip -jrm /tmp/${name}_${dt}.zip /tmp/${name}_${dt}.csv
# 上传文件
/usr/local/python3/bin/python3  /usr/local/airflow/dags/utils/operators/upload_oss.py ${name}/${name}_${dt}.zip /tmp/${name}_${dt}.zip
# zip 文件将在进程退出后被自动清理

#声明文件大小
data_size=$(ls -lh /tmp/${name}_${dt}.zip | awk '{print $5}')
#打印文件信息
Message="${name}_${dt}.zip 上传成功 大小: $data_size"
#配置钉钉告警
curl 'https://oapi.dingtalk.com/robot/send?access_token=816a8f35fd84432ffa6bff9d7714abc322628bb4a8ded86908550d2335aba613' \
   -H 'Content-Type: application/json' \
   -d "
  {\"msgtype\": \"text\",
    \"text\": {
        \"content\": \"监控内容:$Message\"
     }
  }" || true
rm -f  /tmp/${name}_${dt}.txt